<?php

//require_once __DIR__ . '/../../../../../vendor/autoload.php';

//加载GatewayClient。关于GatewayClient参见本页面底部介绍
require_once __DIR__ . '/../../../../../../vendor/workerman/gatewayclient/Gateway.php';

// GatewayClient 3.0.0版本开始要使用命名空间
use GatewayClient\Gateway;
// 设置GatewayWorker服务的Register服务ip和端口，请根据实际情况改成实际值
Gateway::$registerAddress = '127.0.0.1:2346';

if('1'==$_GET['report']){
    error_reporting (0);
    register_shutdown_function($err = 'my_error_handler_TEST') OR set_error_handler($err,E_ALL); // 同时注册两个函数.
}

// 假设用户已经登录，用户uid和群组id在session中
//$uid      = $_SESSION['uid'];
//$group_id = $_SESSION['group'];

$client_id=$_POST['client_id'];
$open_id=$_POST['open_id'];


// client_id与uid绑定
$result=Gateway::bindUid($client_id, $open_id);

Gateway::sendToAll(json_encode(array('type'=>'bing_success', 'open_id'=>$open_id,'client_id'=>$client_id,'result'=>$result,)));

//Gateway::sendToUid($client_id, 'A。已经绑定。OPEN-ID:'.$open_id);

//// 加入某个群组（可调用多次加入多个群组）
//Gateway::joinGroup($client_id, $group_id);



function my_error_handler_TEST($errno=0 ,$errstr=0 , $errfile=0 ,$errline=0){
    if($errno && $errfile){
        if(true){
            $earr = array();
            $earr['type'] = $errno;
            $earr['message'] = $errstr;
            $earr['file'] = $errfile;
            $earr['line'] = $errline;
        }
    }else{
        $earr = error_get_last();
    }
    if(null != $earr){
    echo '<pre>';
    print_r($earr);
    echo '</pre>';
    }
}